package com.zjm.principle.lsp;

/**
 * 违反 里氏替换原则
 */
public class LiskovSubstitution01 {
    public static void main(String[] args) {
        test(new Rectangle()); // 正常输出
        test(new Square());     // 输出“不符合预期！”
    }

    static void test(Rectangle r) {
        r.setWidth(5);
        r.setHeight(4);
        if (r.getArea() != 20)
            System.out.println("不符合预期！");
    }

}


class Rectangle {
    protected int width;
    protected int height;

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getArea() {
        return width * height;
    }
}

class Square extends Rectangle {
    @Override
    public void setWidth(int width) {
        super.setWidth(width);
        super.setHeight(width); // 强制宽高一致
    }

    @Override
    public void setHeight(int height) {
        super.setWidth(height); // 同样设置宽度为高度
        super.setHeight(height);
    }
}
